<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * Description of laporan_model
 *
 * @author Hindarwan
 */
class Laporan_model extends CI_Model {

    var $id_laporan = '';
    var $waktu_laporan = '';
    var $no_toilet = '';
    var $id_kategori_laporan = '';
    var $isi_laporan = '';
    var $id_user = '';

    function __construct() {
        parent::__construct();
    }

    function get_laporan() {
        $query = $this->db->get('laporan');
        return $query;
    }

    function insert() {
        $this->id_laporan = $this->input->post('id_laporan');
        $this->waktu_laporan = $this->input->post('waktu_laporan');
        $this->no_toilet = $this->input->post('no_toilet');
        $this->id_kategori_laporan = $this->input->post('id_kategori_laporan');
        $this->isi_laporan = $this->input->post('isi_laporan');
        $this->id_user = $this->input->post('id_user');

        $this->db->insert('laporan', $this);
    }

    function update($id) {

        $this->id_laporan = $this->input->post('id_laporan');
        $this->waktu_laporan = $this->input->post('waktu_laporan');
        $this->no_toilet = $this->input->post('no_toilet');
        $this->id_kategori_laporan = $this->input->post('id_kategori_laporan');
        $this->isi_laporan = $this->input->post('isi_laporan');
        $this->id_user = $this->input->post('id_user');

        $this->db->where('id', $id);
        $this->db->update('laporan', $this);
    }

    function delete($id) {
        $this->db->where('id', $id);
        $this->db->delete('laporan');
    }

    //another function

    function get_laporan_sorted() {
        $query = $this->db->query('SELECT sekolah.id_sekolah, user.id_user, waktu_laporan, no_toilet, jenis_laporan, nama_user as pelapor, nama_sekolah FROM laporan, kategori_laporan, user, sekolah WHERE laporan.id_user = user.id_user AND sekolah.id_sekolah = user.id_sekolah AND kategori_laporan.id_kategori_laporan = laporan.id_kategori_laporan ORDER BY waktu_laporan DESC');
        return $query;
    }

    function get_laporan_sorted_5() {
        $query = $this->db->query('SELECT sekolah.id_sekolah, user.id_user, waktu_laporan, no_toilet, jenis_laporan, nama_user as pelapor, nama_sekolah FROM laporan, kategori_laporan, user, sekolah WHERE laporan.id_user = user.id_user AND sekolah.id_sekolah = user.id_sekolah AND kategori_laporan.id_kategori_laporan = laporan.id_kategori_laporan ORDER BY waktu_laporan DESC LIMIT 5');
        return $query;
    }

    function get_laporan_by_sekolah($id) {
        $query = $this->db->query('SELECT waktu_laporan, no_toilet, jenis_laporan, nama_user as pelapor, nama_sekolah FROM laporan, kategori_laporan, user, sekolah WHERE laporan.id_user = user.id_user AND sekolah.id_sekolah = user.id_sekolah AND kategori_laporan.id_kategori_laporan = laporan.id_kategori_laporan AND sekolah.id_sekolah = ' . $id . ' ORDER BY waktu_laporan DESC');
        return $query;
    }

    function get_laporan_by_user($id) {
        $query = $this->db->query('SELECT waktu_laporan, no_toilet, jenis_laporan, nama_user as pelapor, nama_sekolah FROM laporan, kategori_laporan, user, sekolah WHERE laporan.id_user = user.id_user AND sekolah.id_sekolah = user.id_sekolah AND kategori_laporan.id_kategori_laporan = laporan.id_kategori_laporan AND user.id_user = ' . $id . 'ORDER BY waktu_laporan DESC');
        return $query;
    }

}

?>
